package com.simplefan.meta.rest;

import net.sf.json.JSONObject;

import com.simplefan.framework.common.rest.RestObjectAnalyzer;
import com.simplefan.meta.MetaObject;
import com.simplefan.meta.cfg.DefaultMetaObject;
import com.simplefan.meta.cfg.SimpleMetaObject;
import com.simplefan.meta.tools.MetaObjectStringConvertor;
import com.simplefan.meta.tools.SimpleMetaObjectJsonUtil;

public class MetaObjectRestObjectAnalyzer implements RestObjectAnalyzer<MetaObject> {

  public MetaObject toValue(String xmlFragment) {
    if(xmlFragment != null && !xmlFragment.equals("null")) {
      return SimpleMetaObjectJsonUtil.toMetaObject(xmlFragment);
    }
    else {
      return null;
    }
  }

  public String toXmlFragment(MetaObject value) {
    SimpleMetaObject metaObject = null;
    if(value instanceof DefaultMetaObject) {
      metaObject = SimpleMetaObject.fromDefaultMetaObject(value);
    }
    else if(value instanceof SimpleMetaObject) {
      metaObject = (SimpleMetaObject)value;
    }

    return MetaObjectStringConvertor.xmlToTXml(JSONObject.fromBean(metaObject).toString());
  }
}
